package com.ruoyi.demo.controller.MeetingRoomAdministrator;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.demo.service.IDeviceLogService;
import com.ruoyi.demo.util.DeviceLogInsertUtil;
import com.ruoyi.system.domain.Device;
import com.ruoyi.system.service.IDeviceService;
import com.ruoyi.system.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @author 33692
 */
@RestController
@RequestMapping("/demo/device")
public class MeetingRoomDeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IDeviceLogService deviceLogService;

    /**
     * 查询对应会议室设备信息
     */
//    @PreAuthorize("@ss.hasPermi('demo:device:list')")
    @GetMapping(value = "/{roomId}")
    public List<Device> getInfo(@PathVariable("roomId") Long roomId)
    {
        List<Device> deviceList = deviceService.selectDeviceListByRoomId(roomId);
        System.out.println(deviceList);
        return deviceList;
    }

    /**
     * 调控设备
     * @param device
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('demo:device:edit')")
    @Log(title = "调控设备", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Device device)
    {
        /**
         * 之后增加python代码调控物理设备
         */
        /**
         * 增加调控日志
         */
        //先获取之前状态
        Device device1 = deviceService.selectDeviceByDeviceId(device.getDeviceId());
        if (deviceService.updateDevice(device) == 1) {
            // 调用工具类记录日志
            DeviceLogInsertUtil.logDeviceChanges(device1, device, deviceLogService, roomService);
        }

        return toAjax(deviceService.updateDevice(device));
    }

    /**
     * 报修设备
     */
//    @PreAuthorize("@ss.hasPermi('demo:device:edit')")
    @Log(title = "报修设备", businessType = BusinessType.UPDATE)
    @PutMapping("/statusEdit")
    public AjaxResult statusEdit(@RequestBody Device device)
    {
        return toAjax(deviceService.updateDevice(device));
    }



}
